package com.aaa.json.contrroller;





import com.aaa.json.entity.Dept;
import com.aaa.json.entity.Emp;
import com.aaa.json.service.DeptService;
import com.aaa.json.service.DeptServiceImpl;
import com.aaa.json.service.EmpService;
import com.aaa.json.service.EmpServiceImpl;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: javamvc-mvcup
 * @description:
 * @author: 诸葛龙辰
 * @create: 2023-03-03 10:37
 * @version:
 **/
//  /EmpServlet/*    * 通配符  通配任何字符串  query  add  toUpdate update  delete......
@WebServlet(name = "EmpServlet", value = "/EmpServlet/*")
public class EmpServlet extends HttpServlet {
    //引入服务层
    private EmpService empService = new EmpServiceImpl();
      DeptService deptService   = new DeptServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求对象的编码格式
        request.setCharacterEncoding("UTF-8");
        //获取url或者uri
        StringBuffer requestURL = request.getRequestURL();
        ////url = http://localhost:16868/EmpServlet/toUpdate   通过url定位到资源  （河南省郑州市高新区科学大道与长椿路交叉口向西200米郑大南门）
        System.out.println("请求的url:" + requestURL);
        String requestURI = request.getRequestURI();
        //        ///uri = EmpServlet/toUpdate    只是资源的名称，无法定位到具体资源    (郑州大学)
        System.out.println("请求的uri:" + requestURI);
        // //截取字符串，获取执行请求的最后部门字符串  requestMethodName = query  add  toUpdate update  delete其中一个
        String requestMethodName = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        //定义返回字符串
        String jsonDate = null;
        //执行各种方法
        switch (requestMethodName) {

            case "queryById":
                jsonDate = this.queryById(request, response);
                break;
            case "add":
                jsonDate = this.add(request, response);
                break;
            case "update":
                jsonDate = this.update(request, response);
                break;
            case "delete":
                jsonDate = this.delete(request, response);
                break;
            case "queryById1":
                jsonDate = this.queryById1(request, response);
                break;
            case "add1":
                jsonDate = this.add1(request, response);
                break;

            case "update1":
                jsonDate = this.update1(request, response);
                break;
            case "delete1":
                jsonDate = this.delete1(request, response);
                break;

            case "queryPage":
                jsonDate =  this.queryPage(request,response);
                break;
            case "queryPage1":
                jsonDate = this.queryPage1(request,response);
                break;

        }
        //判断不null
        if (jsonDate != null) {
            //定义返回类型是json 并且支持中文编码的格式
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println(jsonDate);

        }
    }

    public  String  queryPage(HttpServletRequest request, HttpServletResponse response){
        //接受参数  pageNo   deptName  loc
        String pageNo = request.getParameter("pageNo");
        String empName = request.getParameter("empName");
        String province = request.getParameter("province");

        //封装查询对象
        Emp queryEmp = new Emp();
        queryEmp.setEmpName(empName);
        queryEmp.setProvince(province);

        //定义数字类型 页面
        int pageNoInt = 0;
        //如果第几页没有传递  给默认值
        if(pageNo==null){
            pageNoInt = 1;
        }else {
            //赋值
            pageNoInt = Integer.valueOf(pageNo);
        }
        //判断是否小于1
        if(pageNoInt<1){
            pageNoInt = 1;
        }
        //定义每页显示数量
        int pageSize = 4;
        //查询分页总数量
        int total = empService.queryTotal(queryEmp);
        // 计算最大页
        int maxPage = total%pageSize==0?total/pageSize:(total/pageSize+1);
        //判断是否最大页，如果大于最大页，等于最大页
        if(pageNoInt>maxPage){
            pageNoInt = maxPage;
        }
        //执行分页查询数据
        List<Map> deptMapList = empService.queryByPage(pageNoInt, pageSize, queryEmp);
        //分页查询数据结果
        Map resultMap = new HashMap();
        //分页查询数据结果
        resultMap.put("empMapListA",deptMapList);
        //总条数
        resultMap.put("total",total);
        resultMap.put("maxPage",maxPage);

        /**
         * //放入request对象
         *         request.setAttribute("empMapListA",deptMapList);
         *         //回显数据
         *         request.setAttribute("pageNo",pageNoInt);
         *         request.setAttribute("empName",empName);
         *         request.setAttribute("province",province);
         *         request.setAttribute("total",total);
         *         request.setAttribute("maxPage",maxPage);
         *
         *         return "/emp/list.jsp";
          */
        //把对象转json字符串
        return JSONObject.toJSONString(resultMap);

    }
    public  String  queryPage1(HttpServletRequest request, HttpServletResponse response){
        //接受参数 pageNo deptNo empName salary

        String pageNo = request.getParameter("pageNo");
        String empName = request.getParameter("empName");
       String loc = request.getParameter("loc");
        //封装查询对象
        Dept querDept = new Dept();
        querDept.setEmpName(empName);
        querDept.setLoc(loc);
       // querDept.setSalary(Double.parseDouble(salary));
        //定义数字类型 页面
        int pageNoInt = 0;
        //如果第几页没有传递  给默认值
        if(pageNo==null){
            pageNoInt = 1;
        }else {
            //赋值
            pageNoInt = Integer.valueOf(pageNo);
        }
        //判断是否小于1
        if(pageNoInt<1){
            pageNoInt = 1;
        }
        //定义每页显示数量
        int pageSize = 4;
        //查询分页总数量
        int total = deptService.queryTotal1(querDept);
        //计算最大页
        int maxPage = total%pageSize ==0?total/pageSize:(total/pageSize+1);
        //判断是否最大页，如果大于最大页， 等于最大页
        if(pageNoInt>maxPage){
            pageNoInt = maxPage;
        }
        //执行分页查询数据
        List<Map> deptMapList = deptService.queryByPage1(pageNoInt, pageSize, querDept);
        Map resultMap = new HashMap();
        resultMap.put("empMapListA",deptMapList);
        resultMap.put("total",total);
        resultMap.put("maxPage",maxPage);
//        //放入request对象
//        request.setAttribute("empMapListA",deptMapList);
//        //回显数据
//        request.setAttribute("pageNo",pageNoInt);
//        request.setAttribute("empName",empName);
//       // request.setAttribute("salary",salary);
//        request.setAttribute("total",total);
//        request.setAttribute("maxPage",maxPage);
//
//        return "/emp/list1.jsp";
        //把对象转json字符串
        return JSONObject.toJSONString(resultMap);
    }

    /**
     * 执行查询根据id查询
     *
     * @param request
     * @param response
     * @return
     */
    public String queryById(HttpServletRequest request, HttpServletResponse response) {
        //接受参数 pageNo deptName loc  当然我的emp 查id 是empId
        String empId = request.getParameter("empId");
        //调用服务器根据编号查询对象
        Emp emp = empService.queryById(Integer.valueOf(empId));
        //把对象转json字符串
        return JSONObject.toJSONString(emp);
    }

    /**
     * 执行添加
     *
     * @param request
     * @param response
     * @return
     */
    public String add(HttpServletRequest request, HttpServletResponse response) {

        //接受参数
        String empName = request.getParameter("empName");
        String phoneNum = request.getParameter("phoneNum");
        String deptNo = request.getParameter("deptNo");
        //实例化员工对象
        Emp emp = new Emp(empName, phoneNum, Integer.valueOf(deptNo));
        System.out.println(emp + "12222222222222222222");
        /*Dept dept1 =new Dept();
        dept1.setDeptName(deptName);
        dept1.setLoc(loc);*/
        //调用服务层进行添加
        //实例化服务层对象
        EmpService deptService = new EmpServiceImpl();
        //添加
        int sucNum = deptService.add(emp);
        /**
         * //判断
         *         if (sucNum > 0) {
         *             //重定向到部门查询方法
         *             return "/EmpServlet/query";
         *         } else {
         *             request.setAttribute("error", "操作失败！");
         *             return "/emp/add.jsp";
         *         }
          */
        return  sucNum+"";

    }


    /**
     * 执行更改
     *
     * @param request
     * @param response
     * @return
     */
    public String update(HttpServletRequest request, HttpServletResponse response) {

        //接受参数
        String empId = request.getParameter("empId");
        String empName = request.getParameter("empName");
        String phoneNum = request.getParameter("phoneNum");
        String deptNo = request.getParameter("deptNo");
        //实例化员工对象
        Emp emp = new Emp();
        emp.setEmpId(Integer.valueOf(empId));
        emp.setEmpName(empName);
        emp.setPhoneNum(phoneNum);
        emp.setDeptNo(Integer.valueOf(deptNo));
        /*Dept dept1 =new Dept();
        dept1.setDeptName(deptName);
        dept1.setLoc(loc);*/
        //调用服务层进行添加
        //实例化服务层对象
        EmpService deptService = new EmpServiceImpl();
        //添加
        int sucNum = deptService.update(emp);
//        //判断
//        if (sucNum > 0) {
//            //重定向到部门查询方法
//            return "/EmpServlet/query";
//        } else {
//            request.setAttribute("error", "操作失败！");
//            return "/emp/update.jsp";
//        }
return sucNum+"";
    }

    /**
     *
     *
     * @param request
     * @param response
     * @return 删除
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        //获取编号
        String empId = request.getParameter("empId");
        //实例化业务对象
        EmpService empService = new EmpServiceImpl();
        //调用查询方法
        int sucNum = empService.deleteById(Integer.valueOf(empId));
        //重定向到部门查询方法
        return sucNum+"";
    }
    /**
     * 执行查询
     *
     * @param request
     * @param response
     * @return
     */
    public String queryById1(HttpServletRequest request, HttpServletResponse response) {
        //接受参数 pageNo deptName loc  当然我的emp_salary 查id 是id
        String embId = request.getParameter("embId");
        //调用服务层根据编号查id
        Dept dept = deptService.queryById1(Integer.valueOf(embId));
        System.out.println(dept);
        return JSONObject.toJSONString(dept);
    }

    /**
     * 执行添加
     *
     * @param request
     * @param response
     * @return
     */
    public String add1(HttpServletRequest request, HttpServletResponse response) {

        //接受参数
        String empName = request.getParameter("empName");
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String salary = request.getParameter("salary");
        String loc = request.getParameter("loc");

        //实例化员工对象
        Dept dept = new Dept(empName,Integer.valueOf(year),Integer.valueOf(month),Double.valueOf(salary),loc);
        System.out.println(dept+"12222222222222222222");
        /*Dept dept1 =new Dept();
        dept1.setDeptName(deptName);
        dept1.setLoc(loc);*/
        //调用服务层进行添加
        //实例化服务层对象
        DeptService deptService = new DeptServiceImpl();
        //添加
        int sucNum = deptService.add1(dept);
//        //判断
//        if (sucNum > 0) {
//            //重定向到部门查询方法
//            return "/EmpServlet/queryA";
//        } else {
//            request.setAttribute("error", "操作失败！");
//            return "/emp/add1.jsp";
//        }
        return  sucNum+"";
    }

    /**
     * 跳转更新 （根据编号查询对象）
     *
     * @param request
     * @param response
     * @return
     */



    /**
     * 执行添加
     *
     * @param request
     * @param response
     * @return
     */
    public String update1(HttpServletRequest request, HttpServletResponse response) {

        //接受参数
        String embId = request.getParameter("embId");
        String empName = request.getParameter("empName");
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String salary = request.getParameter("salary");
        String loc = request.getParameter("loc");
        //实例化员工对象
        Dept dept= new Dept();
        dept.setEmbId(Integer.valueOf(embId));
        dept.setEmpName(empName);
        dept.setYear(Integer.valueOf(year));
        dept.setMonth(Integer.valueOf(month));
        dept.setSalary(Double.valueOf(salary));
        dept.setLoc(loc);
        /*Dept dept1 =new Dept();
        dept1.setDeptName(deptName);
        dept1.setLoc(loc);*/
        //调用服务层进行添加
        //实例化服务层对象
        DeptService deptService = new DeptServiceImpl();
        //添加
        int sucNum = deptService.update1(dept);
        //判断
//        if (sucNum > 0) {
//            //重定向到部门查询方法
//            return "/EmpServlet/queryA";
//        } else {
//            request.setAttribute("error", "操作失败！");
//            return "/emp/update1.jsp";
//        }
        return  sucNum+"";
    }

    /**
     * 跳转更新 （根据编号查询对象）
     *
     * @param request
     * @param response
     * @return
     */
    public String delete1(HttpServletRequest request, HttpServletResponse response) {
        //获取编号
        String embId = request.getParameter("embId");
        //实例化业务对象
        DeptService empService = new DeptServiceImpl();
        //调用查询方法
        int sucNum = empService.deleteById1(Integer.valueOf(embId));
        //重定向到部门查询方法
    //    return "/EmpServlet/queryA";
        return  sucNum+"";
    }



}